﻿using MiniFox.Web;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Net;

namespace MiniFox.Platform.Applications.Models
{
    /// <summary>
    /// 系统模块
    /// </summary>
    [JsonObject]
    public abstract class SystemNavModule : SystemNavItem
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Host { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string Port { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public string RootPath { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public bool RequireSSL { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        public bool IsPublic { get; set; }
        /// <summary>
        /// Url参数
        /// </summary>
        [JsonIgnore]
        public string QueryString { get; set; }
        string _url;
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("url")]
        public override string Url
        {
            set { _url = value; }
            get
            {
                //return WebUtil.BuildFullUrl(this.RequireSSL, this.Host, string.IsNullOrEmpty(this.Port)? null : int.Parse(Port), this.RootPath, this._url, this.QueryString, null);
                string fullUrl;
                if (string.IsNullOrWhiteSpace(this._url) || this._url.Trim() == "/" || this._url.Trim() == "~/")
                {
                    fullUrl = this.FullPath;
                }
                else
                {
                    string url = this._url.Trim('/');
                    if (url.StartsWith("http://") || url.StartsWith("https://") || url.StartsWith("ftp://") || url.StartsWith("ftps://"))
                        fullUrl = url;
                    else
                        fullUrl = $"{this.FullPath}/{url.ToLower()}";
                }

                if (!string.IsNullOrWhiteSpace(this.QueryString))
                {
                    fullUrl = fullUrl.AddQueryString(this.QueryString);
                }
                return fullUrl;
            }
        }

        /// <summary>
        /// 获取完整主机名。例: https://www.demo.com 、 http://www.demo.com:8080
        /// </summary>
        [JsonIgnore]
        public string FullHost
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.Host))
                    return string.Empty;
                string host = this.Host.Replace("/", "");
                string protocal = this.RequireSSL ? "https:" : "http:";
                string port = (string.IsNullOrEmpty(this.Port) || this.Port.Trim() == "80") ? string.Empty : (":" + Port);
                return $"{protocal}//{host}{port}".ToLower();
            }
        }

        /// <summary>
        /// 获取应用系统全路径。例: https://www.demo.com 、 http://www.demo.com:8080/system 
        /// </summary>
        [JsonIgnore]
        public string FullPath
        {
            get
            {
                if (string.IsNullOrEmpty(this.RootPath) || this.RootPath.Trim() == "/" || this.RootPath.Trim() == "~/")
                    return FullHost;
                string appPath = this.RootPath.Trim('/');
                return $"{FullHost}/{appPath}".ToLower();
            }
        }
    }

}
